
package simuri.NER;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.TreeSet;
import java.util.regex.Pattern;
import simuri.utils.Configuration;
import simuri.utils.NamedEntity;
import simuri.utils.TurtleReader;


/**
 *
 * @author ngonga
 */
public class Fox implements NerTool{
  
        public TreeSet<NamedEntity> getNEs(String input) {
        TreeSet<NamedEntity> result = new TreeSet<NamedEntity>();
        boolean error = true;
        while (error) {
            try {
                // Construct data
                String data = URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode("text", "UTF-8");
                data += "&" + URLEncoder.encode("task", "UTF-8") + "=" + URLEncoder.encode("ner", "UTF-8");
                data += "&" + URLEncoder.encode("output", "UTF-8") + "=" + URLEncoder.encode("turtle", "UTF-8");
                data += "&" + URLEncoder.encode("task", "UTF-8") + "=" + URLEncoder.encode("ner", "UTF-8");
                data += "&" + URLEncoder.encode("text", "UTF-8") + "=" + URLEncoder.encode(input, "UTF-8");

                // Send data
                URL url = new URL("http://139.18.2.164:4444/api");
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
                wr.flush();

                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String buffer = "", line;
                while ((line = rd.readLine()) != null) {
                    buffer = buffer + line + "\n";
                }

                //read named entities
                result = TurtleReader.read(buffer);
                wr.close();
                rd.close();
                error = false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
   
        return result;
    }
    
//    public TreeSet<NamedEntity> getNEs(String input) {
//     TreeSet<NamedEntity> result = new TreeSet<NamedEntity>();
//      String annotation,  entity;
//        while (input.contains("[") && input.contains("]") ) {
//            annotation = input.substring(input.indexOf("[") + 1, input.indexOf("]"));
//            entity = annotation.substring(0, annotation.indexOf(","));
//            String typ = annotation.substring(annotation.indexOf(",")+ 2,annotation.indexOf(",")+ 5);
//            int index = input.indexOf("[");
//            NamedEntity e = new NamedEntity(entity, typ, index);
//            result.add(e);
//            input = input.replaceFirst(Pattern.quote("[" + annotation + "]"), entity);
//        }
//        return result;
//    }
    
    

    public static void main(String args[]) {
        Fox fox = new Fox();
        long b = System.currentTimeMillis() / 1000;
        //fox.getNEs("The Huffington Post reported Friday that U.S. presidential candidate Buddy Roemer received a $25 campaign donation from an individual listed as Homer Simpson.");
        fox.getNEs("[Ngnadu, LOC, Ngandu] is a settlement in [Kenya, LOC, Kenya]'s [Central Province, LOC, Central_Province_(Kenya)].");
        long e = System.currentTimeMillis() / 1000;
        long runT= e-b;
        //System.out.println("run time is:"fo);
    }

    /** Gets NEs based on the config c
     * 
     * @param c Input config
     * @return All NEs found
     */
    @Override
    public TreeSet<NamedEntity> getNEs(Configuration c) {
        return getNEs(c.inputText);
    }

    /** Annotates all sentence in input file and writes them in output file
     * !!DEPRECATED!!
     * @param input Input file
     * @param output Output file
     */
    public static void annotateAllSentences(String input, String output) {
        Fox fox = new Fox();
        TreeSet<NamedEntity> results;
        try {
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(output)));
            BufferedReader rd = new BufferedReader(new FileReader(input));
            String line = rd.readLine();
            while (line != null) {
                results = fox.getNEs(line);
                System.out.println(results);
                String copy = line + "";
                for (NamedEntity ne : results) {
                    copy = copy.replaceAll(Pattern.quote(ne.label), "[" + ne.label + ", " + ne.type + ", NULL");
                }
                line = rd.readLine();
            }
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
